import numpy as np
import statsmodels.api as sm
import matplotlib.pyplot as plt
from sklearn.datasets import make_moons

def hypothesis(x):
    return np.exp(x)
def SGA(X,y,a=0.001):
    theta=np.zeros(3).T
    for times in range(1000):
        for i in range(100):
            theta+=a*(y[i] - hypothesis(X[i].dot(theta)) )*X[i].T
    return theta
#生成数据
np.random.seed(0)
X,y=make_moons(n_samples=100)
X=np.insert(X,0,1,axis=1)
#梯度上升获得参数，预测
theta=SGA(X,y)
predVals1=hypothesis(np.dot(X,theta))

pModel=sm.Poisson(y,X).fit()
predVals2=pModel.predict(X)

fig,axes=plt.subplots(nrows=1,ncols=2,sharey=True)
axes[0].plot(y, 'r*-', range(len(y)), predVals1, 'bo-.',markersize=3)
axes[0].set_xlabel('Possion in statsmodels')
axes[1].plot(y, 'r*-', range(len(y)), predVals2, 'bo-.',markersize=3)
axes[1].set_xlabel('Possion in our SGA')
fig.suptitle('statsmodels vs. our SGA')
fig.legend(['Real','Predicted'])
plt.show()

